package ru.smc.hogwarts.filters;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import ru.smc.hogwarts.classes.EMF;

public class ActionFilter implements Filter {
	
    public ActionFilter() {
    }

	public void destroy() {
	}
	

	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
		EntityManagerFactory emf = EMF.getInstance().getEMF();
	    EntityManager em = emf.createEntityManager();
	    //Формирование календаря для проверки даты выбора предметов
	    Date date = new Date(); 
		Calendar currentCal = Calendar.getInstance();
		currentCal.setTime(date); 
		Calendar cal = Calendar.getInstance(); 
		cal.setTime(date);
		cal.set(Calendar.MONTH,Calendar.JUNE);
		cal.set(Calendar.DAY_OF_MONTH,1);
		cal.set(Calendar.HOUR, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MILLISECOND, 0); 
		Calendar cal2 = Calendar.getInstance(); 
		cal2.setTime(date);
		cal2.set(Calendar.MONTH,Calendar.JULY);
		cal2.set(Calendar.DAY_OF_MONTH,31);
		cal2.set(Calendar.HOUR, 0);
		cal2.set(Calendar.MINUTE, 0);
		cal2.set(Calendar.SECOND, 0);
		cal2.set(Calendar.MILLISECOND, 0);
		Calendar cal3 = Calendar.getInstance(); 
		cal3.setTime(date);
		cal3.set(Calendar.MONTH,Calendar.SEPTEMBER);
		cal3.set(Calendar.DAY_OF_MONTH,1);
		cal3.set(Calendar.HOUR, 0);
		cal3.set(Calendar.MINUTE, 0);
		cal3.set(Calendar.SECOND, 0);
		cal3.set(Calendar.MILLISECOND, 0);
	    
		Query query = em.createQuery("select COUNT(p.id) from Message p where p.recipient.id =?1 and p.readed=false");
		Query query2 = em.createQuery("select p.id from Message p where  p.recipient.id =?1 and p.readed=false");
		HttpSession session = ((HttpServletRequest) request).getSession();
		int id = 0;
    	try{
    		id=(Integer)session.getAttribute("userID");
        	query.setParameter(1,id);	
        	query2.setParameter(1,id);
        	session.setAttribute("choiceTime",false);
        	if (currentCal.after(cal) && currentCal.before(cal2)) { 
        		session.setAttribute("choiceTime",true);
        	}
        	session.setAttribute("expulsionTime",false);
        	if (currentCal.after(cal2) && currentCal.before(cal3)) { 
        		session.setAttribute("expulsionTime",true);
        	}
        	
        	session.setAttribute("countMess",(Long)query.getSingleResult());
        	session.setAttribute("unreadedMess",(Integer)query2.getSingleResult());
    	}
    	catch (Exception e) {
    	}
		em.close();
		chain.doFilter(request, response);
	}

	public void init(FilterConfig fConfig) throws ServletException {
		
	}

}
